﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using Zeus.Classes.Documents;
using Zeus.Classes;
using Zeus.Classes.Events;
using MenuControl;
using System.Xml.Linq;
using Zeus.Classes.Views;
using Zeus.CompilationService;
using Zeus.UserControls;
using Zeus.Events;
using System.IO.IsolatedStorage;
using System.Windows.Resources;



namespace Zeus.UserControls
{
    public partial class MenuBarUserControl : UserControl
    {
        public MenuBarUserControl()
        {
            InitializeComponent();
        }

        public  int _counterToolBox = 0;
        public  int _counterErrorList = 0;
        public  int _counterSolutionExplorer = 0;
        public int _counterPropertiesPanel = 0;

        private void MenuItem_Click_Save(object sender, RoutedEventArgs e)
        {
            Project.SaveAll();
        }

        private void MenuItem_Click_SaveAs(object sender, RoutedEventArgs e)
        {
            ShowSaveFileDialog();
        }

        private void ShowOpenFileDialog()
        {
            OpenFileDialog openDlg = new OpenFileDialog();
            openDlg.Filter = "Text Files|*.txt|Log Files|*.log|All Files|*.*";
            openDlg.FilterIndex = 2;
            openDlg.ShowDialog();
        }

        private void ShowSaveFileDialog()
        {
            SaveFileDialog saveDlg = new SaveFileDialog();
            saveDlg.ShowDialog();
        }

        private void MenuItemClickImportSound(object sender, RoutedEventArgs e)
        {
            
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == true)
            {

                Stream streamSound = ofd.File.OpenRead();
                SoundDocument soundDocument = new SoundDocument(streamSound);
                soundView soundView = new soundView(soundDocument);
                soundDocument.Lview.Add(soundView);
                soundView.Show();
            }
        }

        private void MenuItem_Click_OpenImage(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openImage = new OpenFileDialog();
            openImage.Filter = "JPEG Files (*.jpg;*.jpeg)|*.jpg;*.jpeg | All Files (*.*)|*.*";
            openImage.ShowDialog();
            Stream stream = openImage.File.OpenRead();      //it crached if i closed dialog without chooseing image

            ImageDocument doc = new ImageDocument(stream);
            DisplayImageView view = new DisplayImageView(doc);
            doc.Lview.Add(view);

            view.Show();
        }

        private void MenuItemClickOpenvideo(object sender, RoutedEventArgs e)
        {
            OpenFileDialog vdialog = new OpenFileDialog();
            vdialog.Filter = "video file(.wmv)|*.wmv";
            if (vdialog.ShowDialog()==true)
            {

                Stream st = vdialog.File.OpenRead();
                videoDocument vdoc = new videoDocument(st);
                viewvideo view = new viewvideo(vdoc);
                view.Show();

            }
            else
            {
                MessageBox.Show("u didn't select a file");
            }
        }
        
        private void MenuItem_Click_NewProject(object sender, RoutedEventArgs e)
        {
            List<XElement> list = GetProjectType();
            //List<XElement> kindlist = GetProjectKind();
            NewProjectChildWindow n = new NewProjectChildWindow(list);
            n.Show();
        }

        private List<XElement> GetProjectType()
        {
            XDocument doc = XDocument.Load("ProjectTypes.xml");
            var projectTypes =
            (from project in doc.Descendants() where project.Name == "type" select project).ToList();
            return projectTypes;
        }
        //private List<XElement> GetProjectKind()
        //{
        //    XDocument doc2 = XDocument.Load("ProjectKind.xml");
        //    var projectkinds =
        //    (from projectkind in doc2.Descendants() where projectkind.Name == "kind" select projectkind).ToList();
        //    return projectkinds;
        //}
       
        
        private void MenuItem_Click_OpenProject(object sender, RoutedEventArgs e)
        {
            OpenProjectChildWindow window = new OpenProjectChildWindow();
            window.Show();
        }

        private void MenuItem_Click_ViewSolutionExplorer(object sender, RoutedEventArgs e)
        {
            if (_counterSolutionExplorer == 0)
            {
                App.EventAggegator.GetEvent<ViewSolutionExplorer>().Publish(null);
                _counterSolutionExplorer++;
            }
            else MessageBox.Show("there are solution explorer already opened");
        }

        private void MenuItem_Click_ViewToolBox(object sender, RoutedEventArgs e)
        {
            if (_counterToolBox == 0)
            {
                App.EventAggegator.GetEvent<ToolBox>().Publish(null);
                _counterToolBox++;
            }
            else
                MessageBox.Show("there are tool box already opened");
        }

        private void MenuItem_Click_ViewErrorList(object sender, RoutedEventArgs e)
        {
            if (_counterErrorList == 0)
            {
                App.EventAggegator.GetEvent<ErrorList>().Publish(null);
                _counterErrorList++;
            }
            else MessageBox.Show("there are error list already opened");
        }

        private void MenuItem_Click_ViewPropertiesPanel(object sender, RoutedEventArgs e)
        {
            if (_counterPropertiesPanel == 0)
            {
                App.EventAggegator.GetEvent<property>().Publish(null);
                _counterPropertiesPanel++;
            }
            else MessageBox.Show("Properties Panel is already opened");
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            Compiler C = new Compiler();
            C.Compile();
        }


        private void MenuItemAddNewItem(object sender, RoutedEventArgs e)
        {
            AddNewItem_ChildWindow addNewItem_ChildWindow = new AddNewItem_ChildWindow();
            addNewItem_ChildWindow.Show();
        }

        private void MenuItemAddExistingItem(object sender, RoutedEventArgs e)
        {
            AddExistingItem_ChildWindow addExistingItem_ChildWindow = new AddExistingItem_ChildWindow();
            addExistingItem_ChildWindow.Show();
           
        }
        
        private void MenuItem_Click_3(object sender, RoutedEventArgs e)
        {
            if(Project.IsSaved().Count>0)
            {    SaveProjectChildWindow saveProjectChildWindow = new SaveProjectChildWindow();
                saveProjectChildWindow.Show();
            }
            else
            {
                Project.CloseSolution(); 
            }
               
        }

        private void MenuItem_Click_4(object sender, RoutedEventArgs e)
        {
            App.EventAggegator.GetEvent<PhysicsTab>().Publish(null);
        }

        private void MenuItem_Click_5(object sender, RoutedEventArgs e)
        {
            Project.AddReference();
        }

        private void MenuItem_Click_6(object sender, RoutedEventArgs e)
        {
            DeleteFilesChildWindow deleteFilesChildWindow = new DeleteFilesChildWindow();
            deleteFilesChildWindow.Show();
           
        }

        private void MenuItem_Click_7(object sender, RoutedEventArgs e)
        {
            Project.DeleteProject();
        }


    }
}
